/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.dailyexpress.mongodbdao;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author GiangLT
 */
public class MongoDbAccess {

    private static MongoDbAccess instance;
    private Datastore datastore;

    private MongoDbAccess() {
        datastore = createDatastore();
    }

    public static synchronized MongoDbAccess getInstance() {
        if (instance == null) {
            instance = new MongoDbAccess();
        }
        return instance;
    }

    private Datastore createDatastore() {
        try {
//            MongoClient mongoClient = new MongoClient("10.54.3.128", 27017);
            MongoClient mongoClient = new MongoClient("192.168.5.33", 27017);
            Morphia morphia = new Morphia();
            return morphia.createDatastore(mongoClient, "smscb");
        } catch (UnknownHostException ex) {
            Logger.getLogger(MongoDbAccess.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public synchronized Datastore getDatastore() {
        if (datastore == null) {
            datastore = createDatastore();
        }
        return datastore;
    }
}
